package com.googlecode.hmvc4gwt.client.module;

import com.googlecode.hmvc4gwt.client.events.in.AddRootPanelViewEvent;
import com.googlecode.hmvc4gwt.client.events.in.RemoveRootPanelViewEvent;
import com.googlecode.hmvc4gwt.client.rootpanel.RootPanelController;
import com.googlecode.hmvc4gwt.client.view.IViewGwt;
import com.googlecode.hmvc4java.component.Component;
import com.googlecode.hmvc4java.component.IComponent;
import com.googlecode.hmvc4java.controller.AController;
import com.googlecode.hmvc4java.event.IEvent;

public abstract class AModuleControllerRootPanel extends AController implements IModuleController {

	private RootPanelController rootPanelController;
	
	/**
	 * @return the rootPanelController
	 */
	private RootPanelController getRootPanelController() {
		return rootPanelController;
	}
	
	/**
	 * @param view
	 * @see com.googlecode.hmvc4gwt.client.rootpanel.RootPanelController#addView(com.googlecode.hmvc4gwt.client.view.IViewGwt)
	 */
	public void addView(IViewGwt view) {
		getRootPanelController().addView(view);
	}

	/**
	 * @param view
	 * @see com.googlecode.hmvc4gwt.client.rootpanel.RootPanelController#removeView(com.googlecode.hmvc4gwt.client.view.IViewGwt)
	 */
	public void removeView(IViewGwt view) {
		getRootPanelController().removeView(view);
	}

	/**
	 * @param rootPanelController the rootPanelController to set
	 */
	protected void setRootPanelController(RootPanelController rootPanelController) {
		this.rootPanelController = rootPanelController;
	}
	
	@Override
	public void onModuleLoad() {
		
		IComponent component = new Component(this);
		component.initComponent();		
	}

	@Override
	public void init() {
		
		setRootPanelController(new RootPanelController());
		
		register(AddRootPanelViewEvent.class);
		register(RemoveRootPanelViewEvent.class);
		
	}

	@Override
	public void terminate() {
		
		terminateImpl();
	}

	@Override
	public void handleEvent(IEvent event) {
		
		if (event instanceof AddRootPanelViewEvent) {
			addView(((AddRootPanelViewEvent) event).getView());
			return;
		} else if (event instanceof RemoveRootPanelViewEvent) {
			removeView(((RemoveRootPanelViewEvent) event).getView());
			return;
		}
		
	}
}
